package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.entity.dto.SysUserDetail;
import com.xinsoft.entity.po.SysUser;
import com.xinsoft.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author xuhb
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2023-04-10 11:34:27
*/
@Service
public class SysUserDetailServiceImpl  implements  UserDetailsService {

    @Resource
    private SysUserService SysUserServiceImpl;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //加载用户基础信息
        QueryWrapper<SysUser> sysUserQuery = new QueryWrapper<>();
        sysUserQuery.eq("username",username);
        sysUserQuery.eq("do_delete",0);
        SysUser users = SysUserServiceImpl.getOne(sysUserQuery);
        SysUserDetail detail = new SysUserDetail();
        if(users!=null){
            BeanUtils.copyProperties(users,detail);
            detail.setId(users.getId());
            //加载用户权限
            List<String> authorities = new ArrayList<>();
            List<String> collect = new ArrayList<>();
            collect.add("ROLE_USER");
            authorities.addAll(collect);
            detail.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(
                    String.join(",",authorities)
            ));
        }
        return detail;
    }
}






